<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');


class User_model extends CI_Model {

	const TBL_USER = 'user';
	
	/**
	 * construct
	 */
	function __construct()
	{
		parent::__construct();
		$this->load->database();
	}

	/**
	 * 获得一条信息
	 * Enter description here ...
	 * @param array() $data
	 */
	function getOne($table,$data,$select = '*',$orderby = ''){
		$row	=	array();
		if($orderby != ''){
			$this->db->order_by($orderby);
		} 
		$query = $this->db->select($select)->get_where($table,$data,1,0);
		if ($query->num_rows() > 0)
		{
		   $row = $query->row_array();
		}
		return $row;
	}

	/**
	 * 添加信息
	 */
	function add_user($data){
	
		$this->db->insert(self::TBL_USER, $data);
		
		return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
	}
	
	function get_user_by_openid ($openid) {
		
		$data = array();
		
		$this->db->select('*')->from(self::TBL_USER)->where('openid', $openid)->limit(1);
		$query = $this->db->get();
		if($query->num_rows() == 1)
		{
			$data = $query->row_array();
		}
		$query->free_result();
		
		return $data;	
	}
	
	function check_openid_exist ($openid) {
		
		$this->db->select('*')->from(self::TBL_USER)->where('openid', $openid);
		$query = $this->db->get();
		
		$num = $query->num_rows();
		
		$query->free_result();
		
		return ($num > 0) ? TRUE : FALSE;
	}

  
}